package net.boyandi.picback;

import java.util.List;
import java.util.Locale;

import javax.swing.table.DefaultTableModel;

import net.boyandi.picback.utils.Files;

import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * @author Ilya Boyandin
 */
public class ScannedFoldersTableModel extends DefaultTableModel {

    private static final long serialVersionUID = 6802568489412120252L;

    private static final DateTimeFormatter DATE_TIME_FORMATTER =
        DateTimeFormat.shortDate().withLocale(Locale.getDefault());

    private List<FolderScanResult> results;

    public void setFolderScanResults(List<FolderScanResult> results) {
        this.results = results;
    }

    @Override
    public int getColumnCount() {
        return 5;
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
        case 0: return "Album"; 
        case 1: return "Starred"; 
        case 2: return "Non-starred"; 
        case 3: return "Date"; 
        case 4: return "Location"; 
        default: return null;
        }
    }
    
    @Override
    public Class<?> getColumnClass(int column) {
        switch (column) {
            case 0: return String.class;
            case 1: return Long.class;
            case 2: return Long.class;
            case 3: return Long.class;
            case 4: return String.class;
            default: return Object.class;
        }
    }
    
    @Override
    public int getRowCount() {
        if (results == null)
            return 0;
        else
            return results.size();
    }
    
    @Override
    public Object getValueAt(int row, int column) {
        if (results == null) return 0;
        FolderScanResult result = results.get(row);
        switch (column) {
        case -1: return result;
        case 0: return result.getName();
        case 1: return new NumAndSize(result.getNumStarred(), result.getSizeStarred());
        case 2: return new NumAndSize(result.getNumNonStarred(), result.getSizeNonStarred());
        case 3: return new DateCell(result.getDate());
        case 4: return result.getPath();
        default: return null;
        }
    }
 
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
    

    private static class NumAndSize implements Comparable<NumAndSize> {
        private final int num;
        private final long size;
        private final String humanReadableSize;
        
        public NumAndSize(int num, long size) {
            this.num = num;
            this.size = size;
            this.humanReadableSize = Files.humanReadableSize(size) + " (" + num + ")";
        }
        
        @Override
        public String toString() {
            return humanReadableSize;
        }

        public int compareTo(NumAndSize o) {
            if (size == o.size) return 0;
            if (size > o.size) return 1;
            return -1;
        }
    }

    private static class DateCell implements Comparable<DateCell> {
        private final LocalDateTime value;
        private final String formatted;
        
        public DateCell(LocalDateTime value) {
            this.value = value;
            this.formatted = DATE_TIME_FORMATTER.print(value);
        }
        
        @Override
        public String toString() {
            return formatted;
        }

        public int compareTo(DateCell o) {
            return value.compareTo(o.value);
        }
    }


}
